package com.litesuits.sdk;

import android.os.Handler;
import android.os.Looper;
import com.litesuits.android.log.Log;

/**
 * @author MaTianyu @http://litesuits.com
 * @date 2015-09-11
 */
public class LiteInitializer {

    private static final String TAG = LiteInitializer.class.getSimpleName();

    public void init(final Runnable subThread, final Runnable uiCallback, final int atLeastMillis) {
        final Handler handler = new Handler(Looper.getMainLooper());
        new Thread(new Runnable() {
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                Log.i(TAG, "init ----------> start");
                subThread.run();
                Log.i(TAG, "init ----------> end ");
                long waitTime = atLeastMillis - (System.currentTimeMillis() - start);
                Log.i("init ----------> done, use time: " + waitTime);
                if (waitTime < 0) {
                    waitTime = 0;
                }
                handler.postDelayed(uiCallback, waitTime);
            }
        }).start();
    }
}
